home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: netcom.com!smryan
- From: smryan@netcom.com (@#$%!?!)
- Subject: Re: Jobs running under Unix.
- Message-ID: <smryanDpzq59.75q@netcom.com>
- Organization: The Programmer formerly known as S M Ryan
- X-Newsreader: TIN [version 1.2 PL1]
- References: <829283553snz@tarik.demon.co.uk>
- Date: Wed, 17 Apr 1996 05:05:33 GMT
- Sender: smryan@netcom8.netcom.com
-
- : Is there anyone who knows how to tell whether a job is running under Unix
- : from within a C program.
-
- : e.g. how do I stop a program from running more than once ?
-
- Rather than directly interrogating running process, try an indirect, and
- time honorred technique: lock files.
-
- Have the program in its initialisation try to lock a file. If it fails, it
- stops immediately. If it does lock the file, let it continue. When it
- completes, remove the lock if you want to permit the program to run again,
- or leave the lock to prevent reruns until the lock is explicitly cleared.
-
- On Unix you can lock with either exclusive create or locking file contents.
- These, or variants of them, can also be used on other systems.
-
- When you create a file, you can mark it as an exclusive create. If the file
- already exists, the create fails. If the file does not exist, the create
- succeeds. Subsequent exclusive creates fail until the file is unlinked. This
- lock is not associated with a specific process and can be useful when
- locking shell scripts or complex programs. The operating systems serialises
- create so simultaneous creates will only be satisfied by one caller.
-
- You can also lock a file contents using fcntl(2). Write lock the entire file
- content. (You're going to have study the man pages.) This lock is process
- based so that if your program ends or aborts without clearing the lock, the
- operating system clears it anyway. Also you can have your program suspended
- until the process holding the lock clears it.
-
- Unix provides other techniques which are not always found on other kinds of
- operating systems, nor always compatiable from one Unix box to another.
- These include shared memory semaphores, message queues, and busy locks or
- existence checks of shared segments. There are also various manners of
- process locks.
- --
- The Queen who loves, the Queen of life, | smryan@netcom.com PO Box 1563
- the Queen who straits, the Queen of strife;| Cupertino, California
- with gasp of death or gift of breath | (xxx)xxx-xxxx 95015
- she brings the choice of birth or knife. | I don't use no smileys
-